package com.sdl.timesblogmp.config;

import com.sdl.timesblogmp.security.JwtInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author sdl
 * @date 2020/5/12 11:12 上午
 * @description
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    private static final List<String> EXCLUDE_PATH = Arrays.asList("/","/css/**","/js/**","/img/**","/fonts/**");
    @Resource
    private JwtInterceptor jwtInterceptor;
    /**
     * 映射文件
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(ProjectConfig.getProfile()).addResourceLocations("file:"+ProjectConfig.getPath());
       // registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    /**
     * 注册了Interceptor后
     * 解决跨域
     *
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");
        config.setAllowCredentials(true);
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }

//    /**
//     * 注册拦截器
//     * @param registry
//     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwtInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns(EXCLUDE_PATH)
//                .excludePathPatterns("/profile/**","/admin/login","/blog/list","/blog/*")
//                .excludePathPatterns("/swagger-resources/**","/webjars/**","/v2/**","/doc.html/**","/main/**");
//    }

}
